home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ue312src.zip / CHAR.C < prev    next >
C/C++ Source or Header  |  1993-04-21  |  9KB  |  323 lines

  1. /*    CHAR.C:    Character handling functions for
  2.         MicroEMACS 3.12
  3.         (C)Copyright 1993 by Daniel Lawrence
  4.  
  5.         ALL THE CODE HERE IS FOR VARIOUS FORMS OF ASCII AND
  6.         WILL HAVE TO BE MODIFIED FOR EBCDIC
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. /*    isletter()
  16.         Is the character a letter?  We presume a letter must
  17.     be either in the upper or lower case tables (even if it gets
  18.     translated to itself).
  19. */
  20.  
  21. int PASCAL NEAR isletter(ch)
  22.  
  23. register unsigned int ch;
  24.  
  25. {
  26.     return(is_upper(ch) || is_lower(ch));
  27. }
  28.  
  29. /*    is_lower()
  30.         Is the character a lower case letter?  This looks
  31.     in the lower to uppercase translation table.
  32. */
  33.  
  34. int PASCAL NEAR is_lower(ch)
  35. register unsigned int    ch;
  36. {
  37.     return(lowcase[ch] != 0);
  38. }
  39.  
  40. /*    is_upper()
  41.         Is the character a upper case letter?  This looks
  42.     in the upper to lowercase translation table.
  43. */
  44.  
  45. int PASCAL NEAR is_upper(ch)
  46. register unsigned int    ch;
  47. {
  48.     return(upcase[ch] != 0);
  49. }
  50.  
  51. /*    chcase()
  52.  
  53.         Change the case of the current character.
  54.     First check lower and then upper.  If it is not a letter,
  55.     it gets returned unchanged.
  56. */
  57.  
  58. unsigned int PASCAL NEAR chcase(ch)
  59. register unsigned int    ch;
  60. {
  61.     /* translate lowercase */
  62.     if (is_lower(ch))
  63.         return(lowcase[ch]);
  64.  
  65.     /* translate uppercase */
  66.     if (is_upper(ch))
  67.         return(upcase[ch]);
  68.  
  69.     /* let the rest pass */
  70.     return(ch);
  71. }
  72.  
  73. /* change *cp to an upper case character */
  74.  
  75. VOID PASCAL NEAR uppercase(cp)
  76.  
  77. unsigned char *cp;    /* ptr to character to uppercase */
  78.  
  79. {
  80.     /* translate uppercase */
  81.     if (is_lower(*cp))
  82.         *cp = lowcase[*cp];
  83. }
  84.  
  85. /* change *cp to an lower case character */
  86.  
  87. VOID PASCAL NEAR lowercase(cp)
  88.  
  89. unsigned char *cp;    /* ptr to character to lowercase */
  90.  
  91. {
  92.     /* translate lowercase */
  93.     if (is_upper(*cp))
  94.         *cp = upcase[*cp];
  95. }
  96.  
  97. #if    PROTO
  98. int PASCAL NEAR upperc(char ch) /* return the upper case equivalant of a character */
  99. #else
  100. int PASCAL NEAR upperc(ch)    /* return the upper case equivalant of a character */
  101.  
  102. unsigned char ch;    /* character to get uppercase euivalant of */
  103. #endif
  104. {
  105.     if (is_lower(ch))
  106.         return(lowcase[ch]);
  107.     else
  108.         return(ch);
  109. }
  110.  
  111. #if    PROTO
  112. int PASCAL NEAR lowerc(char ch) /* return the lower case equivalant of a character */
  113. #else
  114. int PASCAL NEAR lowerc(ch)    /* return the lower case equivalant of a character */
  115.  
  116. unsigned char ch;    /* character to get lowercase equivalant of */
  117. #endif
  118. {
  119.     if (is_upper(ch))
  120.         return(upcase[ch]);
  121.     else
  122.         return(ch);
  123. }
  124.  
  125. VOID PASCAL NEAR initchars()    /* initialize the character upper/lower case tables */
  126.  
  127. {
  128.     register int index;    /* index into tables */
  129.  
  130.     /* all of both tables to zero */
  131.     for (index = 0; index < HICHAR; index++) {
  132.         lowcase[index] = 0;
  133.         upcase[index] = 0;
  134.     }
  135.  
  136.     /* lower to upper, upper to lower */
  137.     for (index = 'a'; index <= 'z'; index++) {
  138.         lowcase[index] = index ^ DIFCASE;
  139.         upcase[index ^ DIFCASE] = index;
  140.     }
  141. #if BSD || USG || AUX || SMOS || HPUX8 || HPUX9 || SUN || XENIX || AVIION
  142.     /* and for those international characters! */
  143.     for (index = (unsigned char)'\340';
  144.          index <= (unsigned char)'\375'; index++) {
  145.         lowcase[index] = index ^ DIFCASE;
  146.         upcase[index ^ DIFCASE] = index;
  147.     }
  148. #endif /*  BSD || USG || AUX || SMOS || HPUX8 || HPUX9 || SUN || XENIX || AVIION */
  149.  
  150. #if    MSDOS
  151.     /* setup various extended IBM-PC characters */
  152.     upcase[0x80]  = 0x87;    /* C with a cedilla */
  153.     lowcase[0x81] = 0x9a;    /* U with an umlaut */
  154.     lowcase[0x82] = 0x90;    /* E with an acute accent */
  155.     lowcase[0x83] = 0x83;    /* A with a circumflex */
  156.     lowcase[0x84] = 0x8e;    /* A with an umlaut */
  157.     lowcase[0x85] = 0x85;    /* A with a grave accent */
  158.     lowcase[0x86] = 0x8f;    /* A with a circle */
  159.     lowcase[0x87] = 0x80;    /* C with a cedilla */
  160.     lowcase[0x88] = 0x88;    /* E with a circumflex */
  161.     lowcase[0x89] = 0x89;    /* E with an umlaut */
  162.     lowcase[0x8a] = 0x8a;    /* E with a grave accent */
  163.     lowcase[0x8b] = 0x8b;    /* I with an umlaut */
  164.     lowcase[0x8c] = 0x8c;    /* I with a circumflex */
  165.     lowcase[0x8d] = 0x8d;    /* I with a grave accent */
  166.     upcase[0x8e]  = 0x84;    /* A with an umlaut */
  167.     upcase[0x8f]  = 0x86;    /* A with a circle */
  168.     upcase[0x90]  = 0x82;    /* E with an acute accent */
  169.     lowcase[0x91] = 0x92;    /* AE diphthong */
  170.     upcase[0x92]  = 0x91;    /* AE diphthong */
  171.     lowcase[0x93] = 0x93;    /* O with a circumflex */
  172.     lowcase[0x94] = 0x99;    /* O with an umlaut */
  173.     lowcase[0x95] = 0x95;    /* O with an acute accent */
  174.     lowcase[0x96] = 0x96;    /* u with a circumflex */
  175.     lowcase[0x97] = 0x97;    /* U with an grave accent */
  176.     lowcase[0x98] = 0x98;    /* y with an umlaut */
  177.     upcase[0x99]  = 0x94;    /* O with an umlaut */
  178.     upcase[0x9a]  = 0x81;    /* U with an umlaut */
  179.     lowcase[0xa0] = 0xa0;    /* a with an acute accent */
  180.     lowcase[0xa1] = 0xa1;    /* i with an acute accent */
  181.     lowcase[0xa2] = 0xa2;    /* o with an acute accent */
  182.     lowcase[0xa3] = 0xa3;    /* u with an acute accent */
  183.     lowcase[0xa4] = 0xa5;    /* n with a tilde */
  184.     upcase[0xa5]  = 0xa4;    /* N with a tilde */
  185. #endif
  186. #if    VMS
  187.     /* setup DEC Multinational Character Set */
  188.     upcase[ 192]  = 224;    /* A with a grave accent */
  189.     upcase[ 193]  = 225;    /* A with an acute accent */
  190.     upcase[ 194]  = 226;    /* A with a circumflex */
  191.     upcase[ 195]  = 227;    /* A with a tilde */
  192.     upcase[ 196]  = 228;    /* A with an umlaut */
  193.     upcase[ 197]  = 229;    /* A with a ring */
  194.     upcase[ 198]  = 230;    /* AE diphthong */
  195.     upcase[ 199]  = 231;    /* C with a cedilla */
  196.     upcase[ 200]  = 232;    /* E with a grave accent */
  197.     upcase[ 201]  = 233;    /* E with an acute accent */
  198.     upcase[ 202]  = 234;    /* E with circumflex */
  199.     upcase[ 203]  = 235;    /* E with an umlaut */
  200.     upcase[ 204]  = 236;    /* I with a grave accent */
  201.     upcase[ 205]  = 237;    /* I with an acute accent */
  202.     upcase[ 206]  = 238;    /* I with circumflex */
  203.     upcase[ 207]  = 239;    /* I with an umlaut */
  204.     upcase[ 209]  = 241;    /* N with a tilde */
  205.     upcase[ 210]  = 242;    /* O with a grave accent */
  206.     upcase[ 211]  = 243;    /* O with an acute accent */
  207.     upcase[ 212]  = 244;    /* O with circumflex */
  208.     upcase[ 213]  = 245;    /* O with a tilde */
  209.     upcase[ 214]  = 246;    /* O with an umlaut */
  210.     upcase[ 215]  = 247;    /* OE ligature */
  211.     upcase[ 216]  = 248;    /* O with a slash */
  212.     upcase[ 217]  = 249;    /* U with a grave accent */
  213.     upcase[ 218]  = 250;    /* U with an acute accent */
  214.     upcase[ 219]  = 251;    /* U with circumflex */
  215.     upcase[ 220]  = 252;    /* U with an umlaut */
  216.     upcase[ 221]  = 253;    /* Y with an umlaut */
  217.  
  218.     lowcase[ 223]  = 223;    /* German lowercase sharp s */
  219.  
  220.     lowcase[ 224]  = 192;    /* a with a grave accent */
  221.     lowcase[ 225]  = 193;    /* a with an acute accent */
  222.     lowcase[ 226]  = 194;    /* a with a circumflex */
  223.     lowcase[ 227]  = 195;    /* a with a tilde */
  224.     lowcase[ 228]  = 196;    /* a with an umlaut */
  225.     lowcase[ 229]  = 197;    /* a with a ring */
  226.     lowcase[ 230]  = 198;    /* ae diphthong */
  227.     lowcase[ 231]  = 199;    /* c with a cedilla */
  228.     lowcase[ 232]  = 200;    /* e with a grave accent */
  229.     lowcase[ 233]  = 201;    /* e with an acute accent */
  230.     lowcase[ 234]  = 202;    /* e with circumflex */
  231.     lowcase[ 235]  = 203;    /* e with an umlaut */
  232.     lowcase[ 236]  = 204;    /* i with a grave accent */
  233.     lowcase[ 237]  = 205;    /* i with an acute accent */
  234.     lowcase[ 238]  = 206;    /* i with circumflex */
  235.     lowcase[ 239]  = 207;    /* i with an umlaut */
  236.     lowcase[ 241]  = 209;    /* n with a tilde */
  237.     lowcase[ 242]  = 210;    /* o with a grave accent */
  238.     lowcase[ 243]  = 211;    /* o with an acute accent */
  239.     lowcase[ 244]  = 212;    /* o with circumflex */
  240.     lowcase[ 245]  = 213;    /* o with a tilde */
  241.     lowcase[ 246]  = 214;    /* o with an umlaut */
  242.     lowcase[ 247]  = 215;    /* oe ligature */
  243.     lowcase[ 248]  = 216;    /* o with a slash */
  244.     lowcase[ 249]  = 217;    /* u with a grave accent */
  245.     lowcase[ 250]  = 218;    /* u with an acute accent */
  246.     lowcase[ 251]  = 219;    /* u with circumflex */
  247.     lowcase[ 252]  = 220;    /* u with an umlaut */
  248.     lowcase[ 253]  = 221;    /* y with an umlaut */
  249.  
  250. #endif
  251. }
  252.  
  253. /*    Set a character in the lowercase map */
  254.  
  255. int PASCAL NEAR setlower(ch, val)
  256.  
  257. char *ch;    /* ptr to character to set */
  258. char *val;    /* value to set it to */
  259.  
  260. {
  261.     return(lowcase[*ch & 255] = *val & 255);
  262. }
  263.  
  264. /*    Set a character in the uppercase map */
  265.  
  266. int PASCAL NEAR setupper(ch, val)
  267.  
  268. char *ch;    /* ptr to character to set */
  269. char *val;    /* value to set it to */
  270.  
  271. {
  272.     return(upcase[*ch & 255] = *val & 255);
  273. }
  274.  
  275. #if (ZTC | TURBO) == 0
  276. /*
  277.  * strrev -- Reverse string in place.  Code here for those compilers
  278.  *    that do not have the function in their own library.
  279.  */
  280. char *strrev(our_str)
  281. char *our_str;
  282. {
  283.     register char    *beg_str, *end_str;
  284.     register char    the_char;
  285.  
  286.     end_str = beg_str = our_str;
  287.     end_str += strlen(beg_str);
  288.  
  289.     do {
  290.         the_char = *--end_str;
  291.         *end_str = *beg_str;
  292.         *beg_str++ = the_char;
  293.     } while (end_str > beg_str);
  294.  
  295.     return(our_str);
  296. }
  297. #endif
  298.  
  299. #if    DBCS
  300. /* is this character a 2 byte character prefix code? */
  301.  
  302. int PASCAL NEAR is2byte(sp, cp)
  303.  
  304. char *sp;    /* ptr to beginning of string containing character to test */
  305. char *cp;    /* ptr to charactor to test */
  306.  
  307. {
  308.     register char *cc;    /* pointer to current character */
  309.  
  310.     cc = sp;
  311.     while (*cc) {
  312.         if (cc > cp)
  313.             return(FALSE);
  314.         if (cc == cp)
  315.             return(is2char(*cp));
  316.         if (is2char(*cc))
  317.             ++cc;
  318.         ++cc;
  319.     }
  320.     return(FALSE);
  321. }
  322. #endif
  323.